package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Order;
import com.itheima.service.AddressService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private AddressDao addressDao;
//分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = addressDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        return new PageResult(total,rows);
    }
//添加
    @Override
    public void add(Address address) {
        String fileName = address.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        addressDao.add(address);
    }
//按id查询
    @Override
    public Address findById(Integer id) {
        Address address = addressDao.findById(id);
        return address;
    }
//编辑
    @Override
    public void edit(Address address) {

        addressDao.edit(address);
    }

    @Override
    public void delete(Integer id) {
        addressDao.delete(id);
    }

    @Override
    public List<Order> findOrderByAddressId(Integer id) {
        List<Order> list = addressDao.findOrderByAddressId(id);
        return list;
    }

    @Override
    public Address findByName(String name) {
        Address address = addressDao.findByName(name);
        return address;
    }

    @Override
    public List<Address> findAll() {
        List<Address> list = addressDao.findAll();
        return list;
    }
}
